package com.hzy.fivepalace.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzy.fivepalace.common.util.ObjectUtils;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.common.util.TableDataInfo;
import com.hzy.fivepalace.db.dao.DtsCommentMapper;
import com.hzy.fivepalace.db.dao.ex.CommentMapperEx;
import com.hzy.fivepalace.db.dao.ex.DtsGoodsMapperEx;
import com.hzy.fivepalace.db.domain.DtsComment;
import com.hzy.fivepalace.db.domain.DtsCommentExample;
import com.hzy.fivepalace.db.domain.DtsGoods;
import com.hzy.fivepalace.db.service.DtsUserService;
import com.hzy.fivepalace.db.vo.wx.WxGoodsCommentVo;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 评价服务
 */
@Service
public class AdminCommentService {

    @Resource
    private CommentMapperEx commentMapperEx;

    @Resource
    private DtsUserService userService;

    @Resource
    private DtsGoodsMapperEx goodsMapperEx;

    @Resource
    private Redisson redisson;

    @Resource
    private DtsCommentMapper commentMapper;


    /**
     * 用户评论列表
     *
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public TableDataInfo getUserCommentList(Integer userId, Integer page, Integer size) {
        Page p = PageHelper.startPage(page, size);
        List<WxGoodsCommentVo> list = commentMapperEx.getUserCommentList(userId);
        List<Map<String, Object>> commentList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> vo = ObjectUtils.convertToMap(item);
            //获取评论用户信息
            vo.put("userInfo", userService.getUserNickNameAndPhoneAndAvatar(item.getCommentUserId()));
            commentList.add(vo);
        });
        return TableDataInfo.getDataTable(commentList, p.getTotal());
    }



    /**
     * 商品评论列表
     *
     * @param goodsId
     * @param page
     * @param size
     * @return
     */
    public TableDataInfo getCommentList(Integer goodsId, Integer page, Integer size) {
        // 商品信息
        DtsGoods info = goodsMapperEx.selectByPrimaryKey(goodsId);
        if (null == info || info.getDeleted()) {
            return TableDataInfo.errorDataTable("商品不存在或已删除");
        }
        Page p = PageHelper.startPage(page, size);
        List<WxGoodsCommentVo> list = commentMapperEx.getCommentListByGoodsId(goodsId);
        List<Map<String, Object>> commentList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> vo = ObjectUtils.convertToMap(item);
            //获取评论用户信息
            vo.put("userInfo", userService.getUserNickNameAndPhoneAndAvatar(item.getCommentUserId()));
            commentList.add(vo);
        });
        return TableDataInfo.getDataTable(commentList, p.getTotal());
    }

    /**
     * 回复评价
     *
     * @param commentId
     * @param replyContent
     * @return
     */
    public Object replyComments(Integer commentId, String replyContent) {
        RLock lock = redisson.getLock("admin_replyComments_" + commentId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        if (StringUtils.isBlank(replyContent)) {
            lock.unlock();
            return ResponseUtil.fail("回复内容不能为空");
        }
        DtsComment comment = commentMapper.selectByPrimaryKey(commentId);
        if (null == comment) {
            lock.unlock();
            return ResponseUtil.fail("评价内容不存在");
        }
        if (comment.getDeleted()) {
            lock.unlock();
            return ResponseUtil.fail("该评价已被删除,无法回复");
        }
        //判断是否回复过
        DtsCommentExample example = new DtsCommentExample();
        example.createCriteria().andDeletedEqualTo(false).andValueIdEqualTo(commentId).andTypeEqualTo((byte) 1);
        if (commentMapper.countByExample(example) > 0) {
            lock.unlock();
            return ResponseUtil.fail("该评价已回复过,无法重复回复");
        }
        DtsComment commentReply = new DtsComment();
        commentReply.setUpdateTime(LocalDateTime.now());
        commentReply.setContent(replyContent);
        commentReply.setType((byte) 1);
        commentReply.setDeleted(false);
        commentReply.setValueId(commentId);
        commentReply.setAddTime(LocalDateTime.now());
        commentMapper.insertSelective(commentReply);
        lock.unlock();
        return ResponseUtil.ok("回复成功", null);

    }
}
